ARG ARCH
FROM harbor.sh.deepin.com/$ARCH/minbase:uos 
RUN apt-get update && apt-get install -y --no-install-recommends git curl ca-certificates unzip xz-utils ssh > /dev/null 2>&1 && \
    useradd rancher && \
    mkdir -p /var/lib/rancher/etcd /var/lib/cattle /opt/jail /opt/drivers/management-state/bin && \
    chown -R rancher /var/lib/rancher /var/lib/cattle /usr/local/bin && \
    rm -rf /var/lib/apt/lists/*
RUN mkdir /root/.kube && \
    ln -s /etc/rancher/k3s/k3s.yaml /root/.kube/k3s.yaml  && \
    ln -s /etc/rancher/k3s/k3s.yaml /root/.kube/config && \
    ln -s /usr/bin/rancher /usr/bin/reset-password && \
    ln -s /usr/bin/rancher /usr/bin/ensure-default-admin && \
    rm -f /bin/sh && ln -s /bin/bash /bin/sh
WORKDIR /var/lib/rancher

ARG ARCH
ARG IMAGE_REPO=rancher
ARG DASHBOARD_BRANCH=release-2.4
# kontainer-driver-metadata branch to be set for specific branch, logic at rancher/rancher/pkg/settings/setting.go
ARG RANCHER_METADATA_BRANCH=release-v2.4

ENV CATTLE_SYSTEM_CHART_DEFAULT_BRANCH=release-v2.4
ENV CATTLE_HELM_VERSION v2.16.8-rancher1
ENV CATTLE_K3S_VERSION v1.17.2+k3s1
ENV CATTLE_MACHINE_VERSION v0.15.0-rancher45
ENV CATTLE_ETCD_VERSION v3.4.3
ENV CATTLE_CHANNELSERVER_VERSION v0.3.0
ENV REPO_SERVER http://10.20.12.249/rancher/deps/$ARCH
ENV LOGLEVEL_VERSION v0.1.3
ENV TINI_VERSION v0.18.0
ENV TELEMETRY_VERSION v0.5.12
ENV KUBECTL_VERSION v1.17.6
ENV DOCKER_MACHINE_LINODE_VERSION v0.1.8
ENV LINODE_UI_DRIVER_VERSION v0.3.0
ENV RANCHER_METADATA_BRANCH release-v2.4
ENV CATTLE_DASHBOARD_INDEX https://releases.rancher.com/dashboard/${DASHBOARD_BRANCH}/index.html
ENV HELM_VERSION v3.2.0
ENV KUSTOMIZE_VERSION v3.5.4

RUN mkdir -p /var/lib/rancher-data/local-catalogs/system-library && \
    mkdir -p /var/lib/rancher-data/local-catalogs/library && \
    mkdir -p /var/lib/rancher-data/local-catalogs/helm3-library && \
    git clone -b $CATTLE_SYSTEM_CHART_DEFAULT_BRANCH --single-branch https://github.com/rancher/system-charts /var/lib/rancher-data/local-catalogs/system-library && \
    git clone -b master --single-branch https://github.com/rancher/charts /var/lib/rancher-data/local-catalogs/library && \
    git clone -b master --single-branch https://github.com/rancher/helm3-charts /var/lib/rancher-data/local-catalogs/helm3-library

RUN curl -sLf ${REPO_SERVER}/machine/${CATTLE_MACHINE_VERSION}/rancher-machine.tar.gz | tar xvzf - -C /usr/bin && \
    curl -sLf ${REPO_SERVER}/loglevel/${LOGLEVEL_VERSION}/loglevel-${LOGLEVEL_VERSION}.tar.gz | tar xvzf - -C /usr/bin && \
    curl -LO ${REPO_SERVER}/docker-machine-driver-linode/${DOCKER_MACHINE_LINODE_VERSION}/docker-machine-driver-linode_linux.zip && \
    unzip docker-machine-driver-linode_linux.zip -d /opt/drivers/management-state/bin && \
    rm docker-machine-driver-linode_linux.zip

ENV TINI_URL ${REPO_SERVER}/tini/${TINI_VERSION}/tini 
ENV HELM_URL_V2 ${REPO_SERVER}/helm/${CATTLE_HELM_VERSION}/rancher-helm
ENV HELM_URL_V3 ${REPO_SERVER}/helm/${HELM_VERSION}/helm-${HELM_VERSION}-linux.tar.gz
ENV TILLER_URL ${REPO_SERVER}/helm/${CATTLE_HELM_VERSION}/rancher-tiller
ENV K3S_URL ${REPO_SERVER}/k3s/${CATTLE_K3S_VERSION}/k3s
ENV CHANNELSERVER_URL ${REPO_SERVER}/channelserver/${CATTLE_CHANNELSERVER_VERSION}/channelserver
ENV ETCD_URL ${REPO_SERVER}/etcd/${CATTLE_ETCD_VERSION}/etcd-${CATTLE_ETCD_VERSION}-linux.tar.gz
ENV KUSTOMIZE_URL ${REPO_SERVER}/kustomize/${KUSTOMIZE_VERSION}/kustomize_${KUSTOMIZE_VERSION}_linux.tar.gz

RUN if [ "${ARCH}" == "arm64" ]; then \
    curl -sLf ${KUSTOMIZE_URL} | tar xvzf - --strip-components=1 -C /usr/bin; chmod +x /usr/bin/kustomize; else \
    curl -sOL ${KUSTOMIZE_URL} && \
    sleep 1 && \
    tar xzf kustomize_${KUSTOMIZE_VERSION}_linux.tar.gz -C /usr/bin && chmod +x /usr/bin/kustomize && \
    rm kustomize_${KUSTOMIZE_VERSION}_linux.tar.gz; \
    fi

# set up helm 2
RUN curl -sLf ${HELM_URL_V2} > /usr/bin/rancher-helm && \
    curl -sLf ${TILLER_URL} > /usr/bin/rancher-tiller && \
    ln -s /usr/bin/rancher-helm /usr/bin/helm && \
    ln -s /usr/bin/rancher-tiller /usr/bin/tiller && \
    chmod +x /usr/bin/rancher-helm /usr/bin/rancher-tiller

# set up helm 3
RUN curl ${HELM_URL_V3} | tar xvzf - --strip-components=1 -C /usr/bin && \
    mv /usr/bin/helm /usr/bin/helm_v3 

RUN curl -sLf ${TINI_URL} > /usr/bin/tini && \
    curl -sLf ${CHANNELSERVER_URL} > /usr/bin/channelserver && \
    curl -sLf ${K3S_URL} > /usr/bin/k3s && \ 
    curl -sfL ${ETCD_URL} | tar xvzf - --strip-components=1 
RUN curl -sLf ${REPO_SERVER}/telemetry/${TELEMETRY_VERSION}/telemetry > /usr/bin/telemetry && \
    curl -sLf ${REPO_SERVER}/kubectl/${KUBECTL_VERSION}/kubectl > /usr/bin/kubectl 

RUN chmod +x /usr/bin/tini /usr/bin/telemetry /usr/bin/k3s /usr/bin/kubectl /usr/bin/channelserver && \
    mv etcd etcdctl /usr/bin/ && \
    mkdir -p /var/lib/rancher-data/driver-metadata

ENV CATTLE_UI_PATH /usr/share/rancher/ui
ENV CATTLE_UI_VERSION 2.4.33
ENV CATTLE_CLI_VERSION v2.4.6

# Please update the api-ui-version in pkg/settings/settings.go when updating the version here.
ENV CATTLE_API_UI_VERSION 1.1.9

RUN mkdir -p /var/log/auditlog
ENV AUDIT_LOG_PATH /var/log/auditlog/rancher-api-audit.log
ENV AUDIT_LOG_MAXAGE 10
ENV AUDIT_LOG_MAXBACKUP 10
ENV AUDIT_LOG_MAXSIZE 100
ENV AUDIT_LEVEL 0
VOLUME /var/log/auditlog

#Consider these UI tarballs are noarch. change to ${REPO_SERVER} if necessary
RUN mkdir -p /usr/share/rancher/ui && \
    cd /usr/share/rancher/ui && \
    curl -sL https://releases.rancher.com/ui/${CATTLE_UI_VERSION}.tar.gz | tar xvzf - --strip-components=1 && \
    mkdir -p assets/rancher-ui-driver-linode && \
    cd assets/rancher-ui-driver-linode && \
    curl -O https://linode.github.io/rancher-ui-driver-linode/releases/${LINODE_UI_DRIVER_VERSION}/component.js && \
    curl -O https://linode.github.io/rancher-ui-driver-linode/releases/${LINODE_UI_DRIVER_VERSION}/component.css && \
    curl -O https://linode.github.io/rancher-ui-driver-linode/releases/${LINODE_UI_DRIVER_VERSION}/linode.svg && \
    mkdir -p /usr/share/rancher/ui/api-ui && \
    cd /usr/share/rancher/ui/api-ui && \
    curl -sL https://releases.rancher.com/api-ui/${CATTLE_API_UI_VERSION}.tar.gz | tar xvzf - --strip-components=1 && \
    cd /var/lib/rancher

ENV CATTLE_CLI_URL_LINUX   ${REPO_SERVER}/rancher/${CATTLE_CLI_VERSION}/rancher-linux-$ARCH-${CATTLE_CLI_VERSION}.tar.gz

ARG VERSION=dev
ENV CATTLE_SERVER_VERSION ${VERSION}
COPY entrypoint.sh rancher /usr/bin/
COPY kustomize.sh /usr/bin/
COPY jailer.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
RUN chmod +x /usr/bin/kustomize.sh

COPY data.json /var/lib/rancher-data/driver-metadata/

#RUN rm -rf /var/lib/rancher/*

ENV CATTLE_AGENT_IMAGE ${IMAGE_REPO}/rancher-agent:${VERSION}
ENV CATTLE_SERVER_IMAGE ${IMAGE_REPO}/rancher
ENV ETCD_UNSUPPORTED_ARCH=${ARCH}
ENV ETCDCTL_API=3

ENV SSL_CERT_DIR /etc/rancher/ssl
VOLUME /var/lib/rancher

ENTRYPOINT ["entrypoint.sh"]
